package model;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public final class JavaArtifactFactory {

	static final Pattern EXT_PATTERN = Pattern.compile(JavaFileExtension.REGEX_FOR_JAVA_FILE_EXTS);
	
	public static final JavaArtifact create(String path) {
		
		String ext = getExtension(path);
		JavaFileExtension javaFileExtension = JavaFileExtension.getJavaFileExtension(ext);
		switch (javaFileExtension) {
		case CLASS:
			ClassArtifact newJavaArtifact = new ClassArtifact(path);
			return newJavaArtifact;
		case JAR:
			JarArtifact jarArtifact = new JarArtifact(path);
			return jarArtifact;
		case WAR:
			return new WarArtifact(path);
		case EAR:
			return new EarArtifact(path);
		default:
			throw new InvalidJavaFilePathException();
		}
	}

	private static String getExtension(String path) {
		Matcher matcher = EXT_PATTERN.matcher(path);
		if(matcher.find()) {
			int start = matcher.start();
			int end = matcher.end();
			return path.substring(start, end);
		}
		else {
			throw new InvalidJavaFilePathException();
		}
	}
	
	public static class InvalidJavaFilePathException extends RuntimeException {

		private static final long serialVersionUID = 1L;
		
	}
	
}
